<?php
class Works_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->database();
    }

    /**
     * Get all Works
     * @return mixed
     */
    function getAll() {
        $this->db->select('*');
        $this->db->from('pr_works');
        $this->db->join('pr_college', 'pr_works.college = pr_college.id', 'left');  // Join two table
        $this->db->join('pr_category', 'pr_works.category = pr_category.id', 'left');
        $query = $this->db->get();
        return $query->result_array();
    }

    /**
     * Insert a new work
     * @param $filename
     */
    function insert($filename) {
        $this->title = $this->input->post('title');
        $this->content = $this->input->post('content');
        $this->realname = $this->input->post('realname');
        $this->studentnum = $this->input->post('studentnum');
        $this->college = $this->input->post('college');
        $this->category = $this->input->post('category');
        $this->filename = $filename;

        $this->db->insert('pr_works', $this);
    }

    /**
     * Select the specific works
     * @return mixed
     */
    function select() {
        $pid = $this->input->get('pid');
        $id = $this->input->get('id');
        $this->db->select('*');
        $this->db->from('pr_works');
        $this->db->where('pid', $pid);
        $this->db->join('pr_college', 'pr_works.college = pr_college.id', 'left');  // Join two table
        $this->db->join('pr_category', 'pr_works.category = pr_category.id', 'left');
        if (!empty($id)) {
            $this->db->where('pr_category.id', $id);
        }
        $query = $this->db->get();
        return $query->row_array();
    }

    /**
     * Edit the specific works
     * @param $filename
     */
    function edit($filename, $category) {
        $pid = $this->input->get('pid');
        if ($category != $this->input->post('category')) {
            $this->category = $this->input->post('category');
            $this->db->update('pr_rates', $this, array('pid' => $pid, 'category'=> $category));
        }

        $this->title = $this->input->post('title');
        $this->content = $this->input->post('content');
        $this->realname = $this->input->post('realname');
        $this->studentnum = $this->input->post('studentnum');
        $this->college = $this->input->post('college');
        $this->category = $this->input->post('category');
        if (!empty($filename))
            $this->filename = $filename;
        $this->db->update('pr_works', $this, array('pid' => $pid));
    }

    /**
     * Delete the specific works
     */
    function delete() {
        $pid = $this->input->get('pid');
        $this->db->delete('pr_works', array('pid' => $pid));

    }

    function pagination() {
        $query = $this->db->query("SELECT * FROM pr_works");
        return $query->num_rows();
    }

    function getByPage($limit, $offset) {
        if ($offset <= 0)
            $offset = 0;
        $this->db->select('*');
        $this->db->join('pr_college', 'pr_works.college = pr_college.id', 'left');  // Join two table
        $this->db->join('pr_category', 'pr_works.category = pr_category.id', 'left');
        $this->db->order_by('pr_works.pid', 'DESC');
        $query = $this->db->get('pr_works', $limit, $offset);
        return $query->result_array();
    }

    function getByCategory($id, $limit, $offset) {
        if ($offset <= 0)
            $offset = 0;
        $this->db->select('*');
        $this->db->where('pr_works.category', $id);
        $this->db->join('pr_college', 'pr_works.college = pr_college.id', 'left');  // Join two table
        $this->db->join('pr_category', 'pr_works.category = pr_category.id', 'left');
        $this->db->order_by('pr_works.pid', 'DESC');
        $query = $this->db->get('pr_works', $limit, $offset);
        return $query->result_array();
    }

    function searchNum($id) {
        $this->db->select('*');
        $this->db->where('pr_works.category', $id);
        $this->db->join('pr_college', 'pr_works.college = pr_college.id', 'left');  // Join two table
        $this->db->join('pr_category', 'pr_works.category = pr_category.id', 'left');
        $this->db->order_by('pr_works.pid', 'DESC');
        $query = $this->db->get('pr_works');
        return  $query->num_rows();
    }
}